package com.adcc.pushXml.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

/**
 * @author LHB
 * @date 2019/11/22 15:44
 */
public class PareseToXml {

    public static  String  objToXml(Object obj) throws JAXBException {
        JAXBContext jc  = JAXBContext.newInstance(obj.getClass());
        Marshaller ma = jc.createMarshaller();
        ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        ma.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        ma.setProperty(Marshaller.JAXB_FRAGMENT, false);
        StringWriter sw = new StringWriter();
        ma.marshal(obj,sw);
        return sw.toString();
    }
}
